package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.OaSecondaryUnit;
import com.ruoyi.system.service.IOaSecondaryUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 二级单位Controller
 *
 * @author ruoyi
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/system/unit1")
public class OaSecondaryUnitController extends BaseController
{
    @Autowired
    private IOaSecondaryUnitService oaSecondaryUnitService;

    /**
     * 查询二级单位列表
     */
    @PreAuthorize("@ss.hasPermi('system:unit1:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaSecondaryUnit oaSecondaryUnit)
    {
        startPage();
        List<OaSecondaryUnit> list = oaSecondaryUnitService.selectOaSecondaryUnitList(oaSecondaryUnit);
        return getDataTable(list);
    }

    /**
     * 导出二级单位列表
     */
    @PreAuthorize("@ss.hasPermi('system:unit1:export')")
    @Log(title = "二级单位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaSecondaryUnit oaSecondaryUnit)
    {
        List<OaSecondaryUnit> list = oaSecondaryUnitService.selectOaSecondaryUnitList(oaSecondaryUnit);
        ExcelUtil<OaSecondaryUnit> util = new ExcelUtil<OaSecondaryUnit>(OaSecondaryUnit.class);
        util.exportExcel(response, list, "二级单位数据");
    }

    /**
     * 获取二级单位详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:unit1:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(oaSecondaryUnitService.selectOaSecondaryUnitById(id));
    }

    /**
     * 新增二级单位
     */
    @PreAuthorize("@ss.hasPermi('system:unit1:add')")
    @Log(title = "二级单位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaSecondaryUnit oaSecondaryUnit)
    {
        return toAjax(oaSecondaryUnitService.insertOaSecondaryUnit(oaSecondaryUnit));
    }

    /**
     * 修改二级单位
     */
    @PreAuthorize("@ss.hasPermi('system:unit1:edit')")
    @Log(title = "二级单位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OaSecondaryUnit oaSecondaryUnit)
    {
        return toAjax(oaSecondaryUnitService.updateOaSecondaryUnit(oaSecondaryUnit));
    }

    /**
     * 删除二级单位
     */
    @PreAuthorize("@ss.hasPermi('system:unit1:remove')")
    @Log(title = "二级单位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(oaSecondaryUnitService.deleteOaSecondaryUnitByIds(ids));
    }
}
